define(['jquery', 'template'], function ($, template) {

    var dialog = function (options) {
        var me = this;
        me.options = $.extend({
            width: 'auto',
            height: 'auto',
            title: '提示',
            content: '',
            ok: true,
            cancel: true,
            okText: '确定',
            cancelText: '取消',
            okFunc: function () {
            },
            cancelFunc: function () {
            },
            events: {
                'afterInit': function () {
                }
            }
        }, options);
        me.init();

    };
    dialog.prototype.init = function () {
        var me = this, opt = me.options;
        var html = '<div class="dialog-cover"></div>'
            + '<div class="dialog" style="{{height}};">'
            + '<div class="dialog-head"><div class="dialog-title">{{#title}}</div><span class="dialog-close">&times</span></div>'
            + '<div class="dialog-body"></div>'
            + '<div class="dialog-foot">'
            + '{{if ok}}'
            + '<div class="dialog-btn-ok">{{okText}}</div>'
            + '{{/if}}'
            + '{{if cancel}}'
            + '<div class="dialog-btn-cancel">{{cancelText}}</div>'
            + '{{/if}}'
            + '</div>'
            + '</div>';

        var dom = $(template.compile(html)(opt));
        var dialog = $('body').append(dom).find('.dialog');
        dialog.find('.dialog-body').append(opt.content);


        // 如果主动设置高度，body部分高度要减去head及foot的高度
        if (opt.height != 'auto') {
            var hfh = dialog.find('.dialog-head').outerHeight() + dialog.find('.dialog-foot').outerHeight();
            dialog.find('.dialog-body').css('height', opt.height - hfh);
        }
        dialog.css({
            'margin-left': -dialog.outerWidth() / 2,
            'margin-top': -dialog.outerHeight() / 2
        });

        dialog.find('.dialog-close').click(function () {
            me.close();
        });
        dialog.find('.dialog-btn-ok').click(function () {
            opt.okFunc();
            me.close();
        });
        dialog.find('.dialog-btn-cancel').click(function () {
            opt.cancelFunc();
            me.close();
        });
        dialog.bind(opt.events);
        dialog.trigger('afterInit');
        me.dialog = dialog;
        me.dom = dom;
    };

    dialog.prototype.show = function () {
        this.dom.show();
    };
    dialog.prototype.close = function () {
        this.dom.remove();
    };
    return {
        show: function (options) {
            return new dialog(options);
        },
        alert: function (options) {
            options = $.extend(options, {
                cancel: false
            });
            return new dialog(options);
        },
        iframe: function (options) {
            options = $.extend(options, {
                content: '<iframe src="' + options.content + '" frameborder="no" scrolling="no" allowtransparency="true" style="width:100%;height:100%;" />',
                ok: false,
                cancel: false
            });
            return new dialog(options);
        }
    };

});